const express = require('express');

const routes = express.Router();

//引入mysql
const mysql = require('mysql');
//创建连接池
var pool = mysql.createPool({
    connectionLimit : 10,
    host : 'localhost',
    user : 'root',
    password : 'root',
    database : 'personmessage'
})

routes.get('/',(req,resp)=>{
    //获取cookies
    //登陆者的工号
    var uid = req.query.uid;
    var month = req.query.month;
    var port_id = req.cookies.port_id;
    var uids = req.cookies.uid;
    var time = req.cookies.getTimes;
    var loginame = req.cookies.loginame;

    console.log(uid , month)
    var hous = new Date(eval(time)).getHours();
    //console.log(hous)
    var minutes = new Date(eval(time)).getMinutes();
    var seconds = new Date(eval(time)).getSeconds();
    var houss = (hous < 10) ? '0'+hous : hous;
    var minus = (minutes < 10) ? '0'+minutes : minutes;
    var secondss = (seconds < 10) ? '0'+seconds : seconds;
    var date = houss+':'+minus+':'+secondss;
    //console.log(date)
    //console.log(houss , minus , secondss)
    //    执行数据查看   `` : 解析字符串里面的变量
    var sql = `SELECT pay.* , permsg.uname FROM pay LEFT JOIN permsg ON permsg.uid = pay.uid WHERE pay.uid = ${uid} AND pay.\`month\` = ${month}`;

    //console.log(sql)
    pool.getConnection(function(err,connection){
        if(err) throw err;
        //    链接成功
        connection.query(sql,(error,result)=> {

            //    释放连接   关闭数据库
            connection.release();

            //判断职务
            if(port_id == 1){
                var port = '普通员工';

            }else if(port_id == 2){
                var port = '行政主管';

            }else if(port_id == 3){
                var port = '财务总监';

            }

            //判断学历
            if(result[0].edu_id == 1){
                var education = '高中';

            }else if(result[0].edu_id == 2){
                var education = '大专';

            }else if(result[0].edu_id == 3){
                var education = '本科';

            }else if(result[0].edu_id == 4){
                var education = '硕士';

            }else if(result[0].edu_id == 5){
                var education = '博士';
            }
            //获取当前时间戳  Date.now()
            var data = new Date().getTime();
            resp.render('pay_update',{
                education : education,  //学历
                port_id : port_id, //职务
                result : result,
                port : port,
                date : date,  //当前时间
                loginame : loginame,
                uids : uids
            })
            //console.log(result)
            //sql语句出错执行报错处理
            if(error) throw error;
        })
    })
})

//编辑
routes.post('/',(req,resp)=> {
    var uid = req.body.uid;
    var month = req.body.month;
    //    月份
    var months = req.body.months;
    //    KBI
    var kbi = req.body.kbi;
    //    提成
    var perfor = req.body.perfor;
    //console.log(months , kbi , perfor , uid , month)
    var sql = `UPDATE pay SET \`month\`=${months} , kbi=${kbi} , perfor=${perfor} WHERE uid = ${uid} AND \`month\`=${month}`;
    //console.log(sql)

    pool.getConnection(function(err,connection){
        if(err) throw err;
        //    链接成功
        connection.query(sql,(error,result)=> {
                //    释放连接   关闭数据库
                connection.release();

                if(result){
                    resp.redirect('sle_msgpay')
                }

            if(error) throw error;
        })
    })
})

//暴露接口
module.exports = routes;


// SELECT stu_list.* , `subject`.sub_name FROM stu_list LEFT JOIN `subject` ON stu_list.sub_id = `subject`.id WHERE stu_list.uid = 1901